//
//  BADetailCellNode.m
//  BabyAlbum
//
//  Created by master on 17/2/27.
//  Copyright © 2017年 master. All rights reserved.
//

#import "BADetailCellNode.h"
#import "BAEditDetailViewNode.h"
#import "BANormalDetailViewNode.h"

@interface BADetailCellNode ()

@property (nonatomic, strong) BASuperDetailNode *detailNode;
@property (nonatomic, strong) UILongPressGestureRecognizer *longPress;

@end

@implementation BADetailCellNode

- (instancetype)initWithData:(BAAlbumModel *)model editing:(BOOL)editing {
    self = [super init];
    self.backgroundColor = [UIColor clearColor];
    self.albumModel = model;
    _isEditing = editing;
    self.clipsToBounds = YES;
    return self;
}

- (void)didLoad {
    [super didLoad];
     [self refreshDetailNode];
}

- (BOOL)save {
    if(![self.detailNode.albumModel.cover isSetted]) {
        [MBProgressHUD showError:@"没有封面"];
        return NO;
    }
    if(![self.detailNode.albumModel.title length]) {
        [MBProgressHUD showError:@"没有标题"];
        return NO;
    }
    
    [BADB updateAlbumDataWithModel:self.detailNode.albumModel];
//    [self back];
    [BAALBUM_DATA reloadData];
    return YES;
}

- (void)reloadData {
    [_detailNode.collectionNode reloadData];
    
}

- (void)setIsEditing:(BOOL)isEditing {
    if(_isEditing != isEditing) {
        _isEditing = isEditing;

        [self refreshDetailNode];
//        [self reloadData];
    }
}

- (void)refreshDetailNode {
    BASuperDetailNode *anotherNode = _isEditing ? [[BAEditDetailViewNode alloc] initWithData:self.albumModel] : [[BANormalDetailViewNode alloc] initWithData:self.albumModel];
    anotherNode.manager = self.manager;
    if(!_isEditing) {
        [anotherNode.view addGestureRecognizer:self.longPress];
    }
    anotherNode.frame = self.bounds;
    anotherNode.view.alpha = 0.3;
    [self addSubnode:anotherNode];
    
    [UIView animateWithDuration:0.5 animations:^{
        _detailNode.view.alpha = 0.3;
        anotherNode.view.alpha = 1;
    } completion:^(BOOL finished) {
        [_detailNode removeFromSupernode];
        _detailNode = anotherNode;
    }];
}

- (UILongPressGestureRecognizer *)longPress {
    if(!_longPress) {
        _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showEdit:)];
        _longPress.minimumPressDuration = 1;
        
    }
    return _longPress;
}

- (void)showEdit:(UILongPressGestureRecognizer *)ges {
    if (ges.state == UIGestureRecognizerStateBegan) {
        UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"编辑" delegate:self cancelButtonTitle:@"取消当前操作" destructiveButtonTitle: nil otherButtonTitles:@"编辑当前相册",@"删除当前相册", nil];
        [actionsheet showInView:KEYWINDOW];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
        {
            self.isEditing = YES;

            if(self.editStatusBlock) {
                self.editStatusBlock(self.isEditing);
            }
        }
            break;
        case 1:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"确定删除" message:@"是否要删除该相册" delegate:nil cancelButtonTitle:@"再想想" otherButtonTitles:@"确定", nil];
            [alert uxy_handlerClickedButton:^(UIAlertView *alertView, NSInteger btnIndex) {
                switch (btnIndex) {
                    case 0:
                        break;
                    case 1: {
                        [self deleteAlbum];
                    }break;
                    default:
                        break;
                }
            }];
            [alert show];
        }
            break;
        case 2:
            
            break;
        default:
            break;
    }
}

- (void)deleteAlbum {
    [BADB deleteAlbum:self.albumModel];
    [BAALBUM_DATA reloadData];
    [self back];
}

- (void)back {
    [self.manager.navigationController popViewControllerAnimated:YES];
}


@end
